package cn.pomelo.signal.controller;

import cn.pomelo.signal.common.AjaxResult;
import cn.pomelo.signal.model.Trade;
import cn.pomelo.signal.model.TradeVo;
import cn.pomelo.signal.service.TradeService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/signal")
public class SignalController {

    @Resource
    private TradeService tradeService;

    @GetMapping("/getAll")
    public AjaxResult getAll(String start, String end, String code) {
        List<Trade> all = tradeService.findAllByDateAndCode(start, end, code);
        return all.size() > 0 ? AjaxResult.success(all) : AjaxResult.failure("请求失败");
    }

    @GetMapping("/getAllTrades")
    public AjaxResult getAllTrades() {
        List<Trade> trades = tradeService.findAllTrades();
        return trades.size() > 0 ? AjaxResult.success(trades) : AjaxResult.failure("请求失败");
    }

    @GetMapping("/getRiseTrades")
    public AjaxResult getRiseTrade(String date) {
        try {
            List<TradeVo> riseTrade = tradeService.getRiseTrade(date);
            return riseTrade == null ? AjaxResult.failure() : AjaxResult.success(riseTrade);
        } catch (ParseException e) {
            return AjaxResult.failure(e.getMessage());
        }
    }
}
